'use strict';
/**
 * 创建用户: 张少辉
 * 日期: 2015-11-20 15:07:43
 * 创建原因: 用于封装localstorage, session,cacheFactory的服务
 * 基于：ngStorage model(ngstorage.js)
 */
angular.module('nec-admin.common')
    .constant('STORAGE_PREFIX', 'necExam-') //存储前缀
    .constant('STORAGE_TYPES', ['$localStorage', '$sessionStorage', '$cacheFactory']) //存储类型
    .config(function($localStorageProvider, $sessionStorageProvider, STORAGE_PREFIX) {
        $localStorageProvider.setKeyPrefix(STORAGE_PREFIX);
        $sessionStorageProvider.setKeyPrefix(STORAGE_PREFIX);
    })
    .provider('storageService', ['STORAGE_TYPES', function(STORAGE_TYPES) {
        var storageType = STORAGE_TYPES[1];

        /**
         * 设置存储类型，用于控制存储的类型
         * @param {[STORAGE_TYPES]} type 存储类型，只能输入STORAGE_TYPES中的类型
         */
        this.setStorageType = function(type) {
            storageType = type;
        };

        this.$get = [storageType, function(storage) {
            var storageService = storage;
            if (typeof storageService === 'object') {
                storageService = {
                    set: function(key, value) {
                        storage[key] = value;
                    },
                    put: function(key, value) {
                        this.set(key, value);
                    },
                    get: function(key, defaultValue) {
                        if (typeof defaultValue !== 'undefined') {
                            storage.$default({
                                key: defaultValue
                            });
                        }
                        return storage[key];
                    },
                    remove: function(key) {
                        delete storage[key];
                    },
                    removeAll: function() {
                        storage.$reset();
                    }
                };
            }
            return storageService;
        }];
    }]);
